package cc.io24.knewcafe.boss.domain;


import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

@Entity
@EntityListeners(AuditingEntityListener.class)
@SQLDelete(sql = "update sys_role set is_deleted = 1 where id = ?")
@Where(clause = "is_deleted = 0")
@org.hibernate.annotations  .Table(appliesTo = "sys_role",comment="系统角色表")
public class SysRole implements Serializable {

    @Id
    @GeneratedValue(generator = "uuid4i")
    @GenericGenerator(name = "uuid4i", strategy = "uuid2")
    @Column(name="id",columnDefinition = "varchar(64) not null comment 'id'")
    private String id ;

    @Column(name="role_name",columnDefinition = "varchar(64) not null comment '角色名'")
    private String roleName ;

    @Column(name="note",columnDefinition = "varchar(255) comment '备注'")
    private String note ;

    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinTable(name="sys_role_permission",
            joinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "permission_id",referencedColumnName = "id")}
    )
    private Set<SysPermission> permissions;

    @Transient
    private Set<String> permission_ids;

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    private Set<SysUser> sysUsers;

    @Column(name = "is_deleted")
    private Integer isDeleted = 0;

    @CreatedDate
    @Column(name = "create_date")
    private Date createDate;

    @LastModifiedDate
    @Column(name = "lastModified_date")
    private Date lastModifiedDate;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Set<SysPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<SysPermission> permissions) {
        this.permissions = permissions;
    }

    public Set<String> getPermission_ids() {
        if(permissions!=null&&permissions.size()!=0){
            if( permission_ids==null){
                permission_ids=new HashSet<>();
            }
            for(SysPermission permission:permissions){
                permission_ids.add(permission.getId());
            }
        }
        return permission_ids;
    }

    public void setPermission_ids(Set<String> permission_ids) {
        if(permission_ids!=null){
            permissions.clear();
            for (String id :permission_ids ) {
                SysPermission permission = new SysPermission();
                permission.setId(id);
                permissions.add(permission);
            }
            this.permission_ids = permission_ids;
        }
    }

    public Set<SysUser> getSysUsers() {
        return sysUsers;
    }

    public void setSysUsers(Set<SysUser> sysUsers) {
        this.sysUsers = sysUsers;
    }

    public Integer getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}
